﻿Imports Microsoft.Office.Interop
Public Class frm_start
    
    Private Sub btn_replace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_replace.Click

        'Dim ex As New Excel.Application
        'Dim pp As New PowerPoint.Application
        Dim unterverz As FileIO.SearchOption
        Me.Label4.Text = "Ersetzen gestartet"
        If Me.cb_unterverzeichnisse.Checked = False Then
            unterverz = FileIO.SearchOption.SearchTopLevelOnly
        Else
            unterverz = FileIO.SearchOption.SearchAllSubDirectories
        End If

        If Me.cb_word.Checked = True Then
            Dim wo As New Word.Application
            Dim wod As Word.Document

            For Each foundFile As String In My.Computer.FileSystem.GetFiles(Me.tb_pfad.Text.ToString, unterverz, "*.doc*")

                wod = wo.Documents.Open(foundFile)
                With wod.Content
                    .Select()

                    .Find.Execute2007(FindText:=Me.tb_suche.Text, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, ReplaceWith:=Me.tb_ersetzen.Text)

                End With

                wod.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges)

            Next
            wo.Quit()
        End If
        If Me.cb_powerpoint.Checked = True Then
            Dim pp As New PowerPoint.Application
            Dim ppd As PowerPoint.Presentation
            Dim sld As PowerPoint.Slide
            Dim shd As PowerPoint.Shape
            Dim oTxtRng As PowerPoint.TextRange
            Dim oTmpRng As PowerPoint.TextRange

            For Each foundFile As String In My.Computer.FileSystem.GetFiles(Me.tb_pfad.Text.ToString, unterverz, "*.ppt*")
                pp.Activate()
                ppd = pp.Presentations.Open(FileName:=foundFile)
                For Each sld In ppd.Slides
                    For Each shd In sld.Shapes
                        If shd.HasTextFrame = Microsoft.Office.Core.MsoTriState.msoTrue Then
                            oTxtRng = shd.TextFrame.TextRange
                            oTmpRng = oTxtRng.Replace(ReplaceWhat:=Me.tb_ersetzen.Text, FindWhat:=Me.tb_suche.Text, WholeWords:=True)
                            Do While Not oTmpRng Is Nothing
                                oTxtRng = oTxtRng.Characters(oTmpRng.Start + oTmpRng.Length, _
                                    oTxtRng.Length)
                                oTmpRng = oTxtRng.Replace(ReplaceWhat:=Me.tb_ersetzen.Text, FindWhat:=Me.tb_suche.Text, WholeWords:=True)
                            Loop

                        End If
                    Next
                Next
                ppd.Save()
                ppd.Close()
            Next
            pp.Quit()

        End If
        If Me.cb_excel.Checked = True Then
            Dim exl As New Excel.Application
            Dim wkb As Excel.Workbook
            Dim wks As Excel.Worksheet
            Dim rag As Excel.Range

            For Each foundFile As String In My.Computer.FileSystem.GetFiles(Me.tb_pfad.Text.ToString, unterverz, "*.xls*")
                wkb = exl.Workbooks.Open(Filename:=foundFile)
                For Each wks In wkb.Worksheets
                    wks.Activate()
                    rag = wks.Range("A1", "XFD1048576")
                    rag.Replace(What:=Me.tb_suche.Text, Replacement:=Me.tb_ersetzen.Text)
                Next
                If wkb.Saved = False Then
                    wkb.Save()
                End If
                wkb.Close()
            Next
            exl.Quit()
        End If
        Me.Label4.Text = "Ersetzen beendet"
    End Sub


    Private Sub frm_start_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Me.cb_excel.Visible = False
        'Me.cb_powerpoint.Visible = False
        Me.tb_pfad.Text = Application.ExecutablePath.ToString
    End Sub
End Class
